home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectInput / DIConfig / flexcheckbox.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  5.2 KB  |  225 lines

  1. //-----------------------------------------------------------------------------
  2. // File: flexcheckbox.cpp
  3. //
  4. // Desc: Implements a check box control similar to Windows check box.
  5. //       CFlexCheckBox is derived from CFlexWnd.  The only place that
  6. //       uses CFlxCheckBox is in the keyboard for sorting by assigned
  7. //       keys.
  8. //
  9. // Copyright (C) 1999-2001 Microsoft Corporation. All Rights Reserved.
  10. //-----------------------------------------------------------------------------
  11.  
  12. #include "common.hpp"
  13.  
  14. CFlexCheckBox::CFlexCheckBox() :
  15.     m_hWndNotify(NULL),
  16.     m_bChecked(FALSE),
  17.     m_rgbText(RGB(255,255,255)),
  18.     m_rgbBk(RGB(0,0,0)),
  19.     m_rgbSelText(RGB(0,0,255)),
  20.     m_rgbSelBk(RGB(0,0,0)),
  21.     m_rgbFill(RGB(0,0,255)),
  22.     m_rgbLine(RGB(0,255,255)),
  23.     m_hFont(NULL),
  24.     m_tszText(NULL)
  25. {
  26. }
  27.  
  28. CFlexCheckBox::~CFlexCheckBox()
  29. {
  30.     delete[] m_tszText;
  31. }
  32.  
  33. void CFlexCheckBox::SetText(LPCTSTR tszText)
  34. {
  35.     LPTSTR tszTempText = NULL;
  36.  
  37.     if (tszText)
  38.     {
  39.         tszTempText = new TCHAR[_tcslen(tszText) + 1];
  40.         if (!tszTempText) return;
  41.         _tcscpy(tszTempText, tszText);
  42.     }
  43.  
  44.     delete[] m_tszText;
  45.     m_tszText = tszTempText;
  46. }
  47.  
  48. void CFlexCheckBox::SetFont(HFONT hFont)
  49. {
  50.     m_hFont = hFont;
  51.  
  52.     if (m_hWnd == NULL)
  53.         return;
  54.  
  55.     Invalidate();
  56. }
  57.  
  58. void CFlexCheckBox::SetColors(COLORREF text, COLORREF bk, COLORREF seltext, COLORREF selbk, COLORREF fill, COLORREF line)
  59. {
  60.     m_rgbText = text;
  61.     m_rgbBk = bk;
  62.     m_rgbSelText = seltext;
  63.     m_rgbSelBk = selbk;
  64.     m_rgbFill = fill;
  65.     m_rgbLine = line;
  66.     Invalidate();
  67. }
  68.  
  69. void CFlexCheckBox::SetRect()
  70. {
  71.     if (m_hWnd == NULL)
  72.         return;
  73.  
  74.     RECT rect = GetRect();
  75.     SetWindowPos(m_hWnd, NULL, rect.left, rect.top, rect.right, rect.bottom, SWP_NOZORDER | SWP_NOMOVE);
  76. }
  77.  
  78. RECT CFlexCheckBox::GetRect(const RECT &rect)
  79. {
  80.     int h = GetTextHeight(m_hFont);
  81.     RECT ret = {rect.left, rect.top, rect.right, rect.top + h + 2};
  82.     return ret;
  83. }
  84.  
  85. RECT CFlexCheckBox::GetRect()
  86. {
  87.     RECT rect;
  88.     GetClientRect(&rect);
  89.     return GetRect(rect);
  90. }
  91.  
  92. void CFlexCheckBox::OnPaint(HDC hDC)
  93. {
  94.     HDC hBDC = NULL, hODC = NULL;
  95.     CBitmap *pbm = NULL;
  96.  
  97.     if (!InRenderMode())
  98.     {
  99.         hODC = hDC;
  100.         pbm = CBitmap::Create(GetClientSize(), RGB(0,0,0), hDC);
  101.         if (pbm != NULL)
  102.         {
  103.             hBDC = pbm->BeginPaintInto();
  104.             if (hBDC != NULL)
  105.             {
  106.                 hDC = hBDC;
  107.             }
  108.         }
  109.     }
  110.  
  111.     InternalPaint(hDC);
  112.  
  113.     if (!InRenderMode())
  114.     {
  115.         if (pbm != NULL)
  116.         {
  117.             if (hBDC != NULL)
  118.             {
  119.                 pbm->EndPaintInto(hBDC);
  120.                 pbm->Draw(hODC);
  121.             }
  122.             delete pbm;
  123.         }
  124.     }
  125. }
  126.  
  127. void CFlexCheckBox::InternalPaint(HDC hDC)
  128. {
  129.     HGDIOBJ hBrush = (HGDIOBJ)CreateSolidBrush(m_rgbBk);
  130.     if (hBrush != NULL)
  131.     {
  132.         HGDIOBJ hOldBrush = SelectObject(hDC, hBrush);
  133.  
  134.         // Erase the background first
  135.         RECT client;
  136.         GetClientRect(&client);
  137.         Rectangle(hDC, client.left, client.top, client.right, client.bottom);
  138.  
  139.         // Create pen for check box
  140.         HGDIOBJ hPen = (HGDIOBJ)CreatePen(PS_SOLID, 1, m_rgbLine);
  141.         if (hPen != NULL)
  142.         {
  143.             HGDIOBJ hOldPen = SelectObject(hDC, hPen);
  144.  
  145.             RECT rect = {0, 0, 0, 0}, textrc;
  146.             GetClientRect(&rect);
  147.             textrc = rect;
  148.             int iBoxDim = rect.bottom - rect.top;
  149.  
  150.             // Draw the square box
  151.             rect.right = rect.left + iBoxDim;
  152.             InflateRect(&rect, -2, -2);
  153.             OffsetRect(&rect, 0, -2);  // Move up to align with the text
  154.             Rectangle(hDC, rect.left, rect.top, rect.right, rect.bottom);
  155.  
  156.             // Draw the check mark if the state is checked.
  157.             if (m_bChecked)
  158.             {
  159.                 HGDIOBJ hCrossPen = CreatePen(PS_SOLID, 3, m_rgbLine);
  160.                 if (hCrossPen != NULL)
  161.                 {
  162.                     SelectObject(hDC, hCrossPen);
  163.                     MoveToEx(hDC, rect.left + 2, rect.top + 2, NULL);  // Upper left
  164.                     LineTo(hDC, rect.right - 2, rect.bottom - 2);  // Lower right
  165.                     MoveToEx(hDC, rect.right - 2, rect.top + 2, NULL);  // Upper right
  166.                     LineTo(hDC, rect.left + 2, rect.bottom - 2);  // Lower left
  167.                     SelectObject(hDC, hPen);
  168.                     DeleteObject(hCrossPen);
  169.                 }
  170.             }
  171.  
  172.             SetBkMode(hDC, TRANSPARENT);
  173.  
  174.             // Draw the message text
  175.             SetTextColor(hDC, m_rgbText);
  176.             textrc.left = rect.right + 8;
  177.             DrawText(hDC, m_tszText, -1, &textrc, DT_LEFT|DT_NOPREFIX|DT_WORDBREAK);
  178.  
  179.             SelectObject(hDC, hOldPen);
  180.             DeleteObject(hPen);
  181.         }
  182.  
  183.         SelectObject(hDC, hOldBrush);
  184.         DeleteObject(hBrush);
  185.     }
  186. }
  187.  
  188. void CFlexCheckBox::Notify(int code)
  189. {
  190.     if (!m_hWndNotify)
  191.         return;
  192.  
  193.     PostMessage(m_hWndNotify, WM_FLEXCHECKBOX,
  194.         (WPARAM)code, (LPARAM)(LPVOID)this);
  195. }
  196.  
  197. void CFlexCheckBox::OnClick(POINT point, WPARAM fwKeys, BOOL bLeft)
  198. {
  199.     if (!m_hWnd)
  200.         return;
  201.  
  202.     RECT rect;
  203.     GetClientRect(&rect);
  204.     rect.right = rect.left + (rect.bottom - rect.top);  // Adjust the width to same as height.
  205.     InflateRect(&rect, -2, -2);
  206.     OffsetRect(&rect, 0, -2);  // Move up to align with the text
  207.     if (PtInRect(&rect, point))
  208.     {
  209.         m_bChecked = !m_bChecked;
  210.         Invalidate();
  211.         Notify(m_bChecked ? CHKNOTIFY_CHECK : CHKNOTIFY_UNCHECK);  // Notify the page object about the state change.
  212.     } else
  213.     {
  214.         // Unhighlight current callout
  215.         HWND hWndParent;
  216.         hWndParent = GetParent(m_hWnd);
  217.         SendMessage(hWndParent, WM_UNHIGHLIGHT, 0, 0);  // Send click message to page to unhighlight callout
  218.     }
  219. }
  220.  
  221. void CFlexCheckBox::OnMouseOver(POINT point, WPARAM fwKeys)
  222. {
  223.     Notify(CHKNOTIFY_MOUSEOVER);
  224. }
  225.